package com.hertz.iris.view.alarm;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.actionbarsherlock.app.SherlockFragment;
import com.hertz.iris.R;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.ui.CardFlipHelper;
import com.hertz.iris.util.BundleKeys;
import com.hertz.iris.util.Utils;

public class LoopSelectorFrag extends SherlockFragment{
	
	CardFlipHelper cardFlipHelper = new CardFlipHelper();
	
	@InjectView(R.id.mon_btn) Button monBtn;
	@InjectView(R.id.tues_btn) Button tuesBtn;
	@InjectView(R.id.wed_btn) Button wedBtn;
	@InjectView(R.id.thur_btn) Button thurBtn;
	@InjectView(R.id.fri_btn) Button friBtn;
	@InjectView(R.id.sat_btn) Button satBtn;
	@InjectView(R.id.sun_btn) Button sunBtn;
	
	private OnLoopSelectLisener myCallback;
	private byte[] values = {0,0,0,0,0,0,0};
	private Button[] buttons;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			myCallback = (OnLoopSelectLisener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement onLoopSelectLisener");
        }
	}
	
	@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) { 
		super.onCreateView(inflater, container, savedInstanceState);
        
		View v = inflater.inflate(R.layout.loop_selector_frag, container, false);
		ButterKnife.inject(this,v);
		buttons = new Button[]{monBtn,tuesBtn,wedBtn,thurBtn,friBtn,satBtn,sunBtn};
		initWeeks();
        return v;
    }  
	
	void initWeeks(){
		Bundle ars = getArguments();
        NormalAlarm na = (NormalAlarm)ars.getSerializable(BundleKeys.NormalAlarmBundleKeys.NORMAL_ALARM_KEY);
		values = Utils.string2byteArr(na.getWeeks(), ",");
		for(byte i = 0; i < values.length; i++){
			if(values[i] == 1){
				Button b = buttons[i];
				changeSelected(b);
				b.setTextColor(getResources().getColor(R.color.bbutton_success));;
			}
		}
		
	}

	@OnClick(value={R.id.mon_btn,R.id.tues_btn,R.id.wed_btn,R.id.thur_btn,R.id.fri_btn,R.id.sat_btn,R.id.sun_btn}) 
	void clickBtn(Button button){
		changeSelected(button);
		byte value = getSelectedValue(button);
		switch (button.getId()) {
		case R.id.mon_btn:
			values[0] = value;
			break;
		case R.id.tues_btn:
			values[1] = value;
			break;
		case R.id.wed_btn:
			values[2] = value;
			break;
		case R.id.thur_btn:
			values[3] = value;
			break;
		case R.id.fri_btn:
			values[4] = value;
			break;
		case R.id.sat_btn:
			values[5] = value;
			break;
		case R.id.sun_btn:
			values[6] = value;
			break;
		}
		cardFlipHelper.packCardFlip(CardFlipHelper.DEFAULT_THEME, button);
		myCallback.loopSelectListener(values);
	}
	
	private byte getSelectedValue(Button button){
		byte value = (byte)0;
		if(button.isSelected()){
			value = 1;
		}
		return value;
	}
	
	private void changeSelected(Button button){
		if(button.isSelected()){
			button.setSelected(false);
		}else{
			button.setSelected(true);
		}
	}
	
	public interface OnLoopSelectLisener{
		public void loopSelectListener(byte[] values);
	}
	
}
